package com.pay.controller.admin;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pay.common.base.BaseController;
import com.pay.model.custom.TableSplitResult;
import com.pay.model.custom.Tablepar;
import com.pay.model.custom.TitleVo;
import com.pay.model.custom.TsysTables;
import com.pay.service.GeneratorService;
import com.github.pagehelper.PageInfo;


/**
 * 自动生成Controller
 *
 * @author fuce  
 * @version V1.0   
 * @Title: GeneratorController.java 
 * @Package com.fc.test.controller.admin 
 * @date 2019年5月9日 上午12:40:47 
 */
@Controller
@RequestMapping("generatorController")
public class GeneratorController extends BaseController {
    @Autowired
    private GeneratorService generatorService;

    private String prefix = "admin/generator";

    @GetMapping("view")
    @RequiresPermissions("system:generator:view")
    public String view(Model model) {
        String str = "自动生成";
        setTitle(model, new TitleVo("列表", str + "管理", true, "欢迎进入" + str + "页面", true, false));
        return prefix + "/list";
    }


    @PostMapping("list")
    @RequiresPermissions("system:generator:list")
    @ResponseBody
    public Object list(Tablepar tablepar, String searchTxt) {
        PageInfo<TsysTables> page = generatorService.list(tablepar, searchTxt);
        TableSplitResult<TsysTables> result = new TableSplitResult<TsysTables>(page.getPageNum(), page.getTotal(), page.getList());
        return result;
    }


    /**
     * 生成代码
     */
    @GetMapping("/code")
    public void code(String tables, HttpServletResponse response) throws IOException {
        byte[] data = generatorService.generatorCode(tables.split(","));

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"SpringBootV2.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
